//动画函数
export function animation(opt: {
  from: number;
  to: number;
  duration?: number;
  onUpdate?: (val: number) => void;
  onEnd?: () => void;
}) {
  const { from, to, duration = 1000, onUpdate, onEnd } = opt;
  let value = from;
  const start = Date.now();
  const speed = (to - from) / duration;

  function _run() {
    const t = Date.now() - start;
    if (t > duration) {
      value = to;
      onUpdate?.(value);
      onEnd?.();
      return;
    }
    value = from + t * speed;
    onUpdate?.(value);
    requestAnimationFrame(_run);
  }
  requestAnimationFrame(_run);
}
